package com.moxiao.yfjh.module.blind.controller.app.controller.user;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.module.blind.common.GenNoCommonMethod;
import com.moxiao.yfjh.module.blind.controller.admin.memberReport.vo.MemberReportCreateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.blind.service.memberReport.MemberReportService;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.error;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/2/18 17:58
 **/
@Tag(name = "APP - 相亲交友举报")
@RestController
@RequestMapping("/blind/member-report")
@Validated
public class AppMemberReportController {

    @Resource
    private MemberReportService memberReportService;

    @Resource
    private BlindMemberUserService blindMemberUserService;

    @Resource
    private GenNoCommonMethod genNoCommonMethod;

    @PostMapping("/createMemberReport")
    @Operation(summary = "创建相亲交友举报")
    public CommonResult<Long> createMemberReport(@Valid @RequestBody MemberReportCreateReqVO createReqVO) {
        Long loginUserId = getLoginUserId();
        if (Objects.isNull(loginUserId)){
            return error(401, "获取个人信息失败");
        }

        genNoCommonMethod.addReportNo(createReqVO);
        createReqVO.setReportId(loginUserId);
        List<Long> arrayList = new ArrayList<>();
        arrayList.add(getLoginUserId());
        arrayList.add(createReqVO.getQuiltReport());
        List<BlindMemberUserDO> memberUserList = blindMemberUserService.getMemberUserList(arrayList);
        if (CollectionUtils.isNotEmpty(memberUserList)){
            memberUserList.forEach(blindMemberUserDO -> {
                if (Objects.equals(blindMemberUserDO.getId(),getLoginUserId())){
                    createReqVO.setReportName(blindMemberUserDO.getNickname());
                }else {
                    createReqVO.setQuiltName(blindMemberUserDO.getNickname());
                }
            });
        }
        if (CollectionUtils.isNotEmpty(createReqVO.getEvidenceImgList())){
            createReqVO.setEvidenceImg(JSON.toJSONString(createReqVO.getEvidenceImgList()));
        }
        if (CollectionUtils.isNotEmpty(createReqVO.getEvidenceVideoList())){
            createReqVO.setEvidenceVideo(JSON.toJSONString(createReqVO.getEvidenceVideoList()));
        }
        return success(memberReportService.createMemberReport(createReqVO));
    }

}
